define(['jquery' , 'datatables.net'] , function(){

	var dtoUtil = {
		postDT 	:　	function( sSource , param , aoData , fnCallback ){
			if( sSource instanceof Array ){
				fnCallback( dtoUtil.handleRsult( {
					data 	: 	sSource ,
					extra 	: 	sSource.length
				} ) ) ;
				return ;
			}
			dtoUtil.request = aoData ;
			var standardParam = dtoUtil.standardReqDT( aoData ) ;
			if( param ){
				for( var key in param ){
					standardParam[ key ] = param[key] ;
				};
			}
			console.log('------search param----------\n' , standardParam , param , !param ) ;
			$.ajax({
				url 	: 	sSource ,
				type 	: 	'GET' ,
				async 	: 	true ,
				data 	: 	standardParam ,
				success : 	function( result , status , xmlHttp ){
					if( typeof result == 'string' )
						result = JSON.parse( result ) ;
					if( result.code != 0 ){
						fnCallback( dtoUtil.handleRsult( {} ) ) ;
						return alert( result.message ) ;
					}
					console.log("返回结果", result ) ;
					fnCallback( dtoUtil.handleRsult( result ) ) ;
				},
				error	: 	function( xmlHttp , status ){
					alert('服务器请求出错:CODE('+ xmlHttp.status +')!') ;
					fnCallback({}) ;
				}
			});
		},
		standardReqDT 	: 	function( aoData ){
			if( !aoData || !aoData.length ){
				return aoData ;
			}
			var temp = {} ;
			for( var key in aoData ){
				temp[aoData[key].name] = aoData[key].value ;
			}
			var standardParam = {
				_size 	: 	temp['iDisplayLength'] ,
				_page  	:　	temp['iDisplayStart'] / temp['iDisplayLength'] + 1,
				_sort 	: 	temp['mDataProp_'+temp['iSortCol_0']] ,
				_by 	: 	temp['sSortDir_0']
			};
			return standardParam ;
		},
		handleRsult 	: 	function( result ){
			var preview = dtoUtil.request || {} ;
			var aoData = {
				sEcho 	: 	preview.sEcho + 1,
				iDisplayStart 	: 	preview.iDisplayStart ,
				iDisplayLength 	: 	preview.iDisplayLength ,
				iTotalRecords 	: 	result.extra ? result.extra : 0 ,
				iTotalDisplayRecords 	: 	result.extra ? result.extra : 0 ,
				aaData 	: 	result.data ? result.data : [] 
			};
			return aoData ;
		}
	};
	var TableConfig = function(){
		var _this = this;
		this.setting = {
			oLanguage 	: 	{
				"sLengthMenu" : "每页显示 _MENU_ 条记录",
				"sZeroRecords" : "暂无数据",
				"sInfo" : "共有 _TOTAL_ 条记录",
				"sInfoEmtpy" : "没有数据",
				"sProcessing" : "加载中...请稍等",
				"search" : "查询",
				"sInfoEmpty" : "共 0 条记录",
				"bStateSave" : false,
				"oPaginate" : {
					"sFirst" : "首页",
					"sPrevious" : "前页",
					"sNext" : "后页",
					"sLast" : "尾页"
				}
			},
			paging				:   true,
			sPaginationType 	: 	'full_numbers' ,
			bJQueryUI 			: 	false ,
			bAutoWidth			: 	false ,
			bProcessing			: 	true ,
			sServerMethod 		: 	'GET' ,
			bServerSide			: 	true ,
			bFilter				: 	false ,
			bStateSave 			: 	true ,
			aLengthMenu 		: 	[ [ 10, 25, 50 ], [ 10, 25, 50 ] ] ,
			aaSorting 			: 	[[0,'desc']] ,
			destroy 			: 	true ,
			fnDrawCallback 		: 	function(){
				//console.log('Data Table CallBack ...') ;
				
			},
			fnInitComplete		: 	function( oSetting , json ){
				//console.log('Data Table Init Complete ...') ;
			},
			fnRowCallback 		: 	function( nRow , aData , iDisplayIndex ){
				//console.log( 'Data Table Row Call Back ...') ;
			},
			fnServerData 		: 	function( sSource , aoData , fnCallback ){
				console.log('---settingparam---' , _this.setting )
				 sSource = _this.setting.ajaxSource || sSource ;
				 console.log('列表请求:' , sSource ) ;
				dtoUtil.postDT( sSource  , _this.setting.param , aoData , fnCallback ) ;
			},
			param : 	{} 
		} ;
	}

	var controller = function( $scope , $element , $timeout ){

		var setting = new TableConfig().setting;
		

		setting.param = $scope.searchParam || {} ;
		$scope.searchParam = setting.param ;
		
		$scope.setting = $.extend( true  , setting , $scope.setting );
		$scope.$watch( 'setting.ajaxSource' , function( n , o ){
			console.log( 'setting 监听' , n , o ) ;
		}  , true );

		$scope.$watch('searchParam' , function( n , o ){
			console.log('查询参数:' , o , n , setting.param ) ;
		},true);

		$scope.$watch('setting.ajaxSource' , function( n , o ){
			console.log('请求地址:' , o , n , setting.ajaxSource ) ;
			setting.ajaxSource = n ;
			//设置请求地址时候初始化列表.
			if( $scope.setting.ajaxSource ){
				console.log('-----------------------000000000000000000000----------------------------')
				$scope.table  = $( $element ).DataTable( $scope.setting ) ;
			}	
		});
	}

	return app.register.directive('ngDatatable' , function(){
		return {
			restrict 	: 	'E' ,
			replace 	: 	true ,
			scope 		: 	{
				setting : 	'=setting' ,
				table 	: 	'=table' ,
				searchParam : '=searchParam'
			},
			templateUrl : 	'assets/modules/ng-datatable/template.html' ,
			controller 	: 	controller 
		}
	});

});